package com.yaowk.common.kit;

import com.jfinal.core.Controller;
import com.jfinal.kit.JMap;

/**
 * 返回值工具类
 * Created by yaowk on 2017/4/16.
 */
public class ResultKit {

    public static JMap success() {
        return success("");
    }

    public static JMap success(Controller c) {
        return success(I18nKit.get("success", Constant.COMMON_MODULE, c));
    }

    public static JMap success(String msg) {
        return success(msg, ServiceCode.SUCCESS);
    }

    public static JMap success(String msg, String code) {
        return success(null, msg, code);
    }

    public static JMap success(Object data) {
        return success(data, "");
    }

    public static JMap success(Object data, String msg) {
        return success(data, msg, ServiceCode.SUCCESS);
    }

    public static JMap success(Object data, String msg, String code) {
        return result(data, msg, code, true);
    }

    public static JMap successPaging(Object data, int currentPage, int pageSize, int totalCount, String msg, String code) {
        return paging(data, currentPage, pageSize, totalCount, msg, code, true);
    }

    public static JMap successPaging(Object data, int currentPage, int pageSize, int totalCount, String msg) {
        return successPaging(data, currentPage, pageSize, totalCount, msg, ServiceCode.SUCCESS);
    }

    public static JMap successPaging(Object data, int currentPage, int pageSize, int totalCount) {
        return successPaging(data, currentPage, pageSize, totalCount, "");
    }

    public static JMap fail(Controller c) {
        return success(I18nKit.get("error", Constant.COMMON_MODULE, c));
    }

    public static JMap fail() {
        return fail("");
    }

    public static JMap fail(String msg) {
        return fail(msg, ServiceCode.ERROR);
    }

    public static JMap fail(String msg, String code) {
        return fail("", msg, code);
    }

    public static JMap fail(Object data) {
        return fail(data, "");
    }

    public static JMap fail(Object data, String msg) {
        return fail(data, msg, ServiceCode.ERROR);
    }

    public static JMap fail(Object data, String msg, String code) {
        return result(data, msg, code, false);
    }

    public static JMap failPaging(Object data, int currentPage, int pageSize, int totalCount, String msg, String code) {
        return paging(data, currentPage, pageSize, totalCount, msg, code, false);
    }

    public static JMap failPaging(Object data, int currentPage, int pageSize, int totalCount, String msg) {
        return failPaging(data, currentPage, pageSize, totalCount, msg, ServiceCode.ERROR);
    }

    public static JMap failPaging(Object data, int currentPage, int pageSize, int totalCount) {
        return failPaging(data, currentPage, pageSize, totalCount, "");
    }

    public static JMap failPaging(int currentPage, int pageSize, int totalCount) {
        return failPaging("", currentPage, pageSize, totalCount);
    }

    private static JMap result(Object data, String msg, String code, boolean success) {
        JMap result;
        if (success) {
            result = JMap.ok("msg", msg);
        }
        else {
            result = JMap.fail("msg", msg);
        }
        result.set("code", code)
                .set("data", data);
        return result;
    }

    private static JMap paging(Object data, int currentPage, int pageSize, int totalCount, String msg, String code, boolean success) {
        JMap jMap = JMap.create("totalCount", totalCount)
                .set("currentPage", currentPage)
                .set("pageSize", pageSize)
                .set("data", data);
        return result(jMap, msg, code, success);
    }
}
